package com.bacel.nb.product.web.util;

import com.alibaba.fastjson.JSON;
import com.bacel.nb.product.core.domain.Syslog;
import com.bacel.nb.product.core.domain.Sysmodule;
import com.bacel.nb.product.core.domain.Sysuser;
import com.bacel.nb.product.core.dto.ListGageModule;
import com.bacel.nb.product.core.dto.sysmoduleDto;
import com.bacel.nb.product.core.service.ILogsService;
import com.bacel.nb.product.core.service.ISysmoduleService;
import com.bacel.nb.product.core.service.impl.SysmoduleService;
import com.bacel.nb.product.web.enums.CacheKey;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Bacel on 2017/4/5.
 */
public class WebUtilify {
    public static String LoginUserKey = "LoginUserKey";
    public static String ClientCkey = "123kljfd";

    //获取登录用户信息
    public static Sysuser LoginUserinfo(HttpServletRequest request) {
        Sysuser user = new Sysuser();
        HttpSession session = request.getSession();
        Object pObj = session.getAttribute(WebUtilify.LoginUserKey);
        if (pObj != null) {
            user = (Sysuser) pObj;
        }
        return user;
    }

    public static void WriteLog(Syslog log,ILogsService iLogsService) {
        iLogsService.insertT(log);
    }

    public static String BuildMenuinfo(String nowpath,ISysmoduleService sysmoduleService) {
        String re = "";
        ListGageModule modulelist = GetDataModuleAll(sysmoduleService);
        return re;
    }

    public static ListGageModule GetDataModuleAll(ISysmoduleService sysmoduleService) {

        //TODO
        ListGageModule datamodule = null;
        try {
            CacheOperation co = CacheOperation.getInstance();
            datamodule = (ListGageModule) co.getCacheData(CacheKey.sysmodule.toString(), 0, 0);
            if (datamodule == null) {
                datamodule = new ListGageModule();
                List<Sysmodule> list = sysmoduleService.getList();
                datamodule.setList(list);
                if (datamodule != null && (datamodule.getErrstr() == null || datamodule.getErrstr().length() == 0)) {
                    co.addCacheData(CacheKey.sysmodule.toString(), datamodule);
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        return datamodule;
    }

    //循环设置module的子项
    public static sysmoduleDto setChildren(List<sysmoduleDto> list, sysmoduleDto module) {
        List<sysmoduleDto> golist = new ArrayList<>();
        List<sysmoduleDto> mylist = GetModule(list, module.getId().toString(), 0);
        if (mylist != null && mylist.size() > 0) {
            for (int i = 0; i < mylist.size(); i++) {
                sysmoduleDto cm = setChildren(list, mylist.get(i));
                golist.add(cm);
            }
        }
        module.setChildren(golist);
        return module;
    }

    public static List<sysmoduleDto> getListChildren(sysmoduleDto data, int DeepNum) {
        List<sysmoduleDto> lst = new ArrayList<>();
        if (data.getChildren() == null || data.getChildren().size() == 0)
            return lst;
        for (int i = 0; i < data.getChildren().size(); i++) {

            String DeepValue = "";
            for (int j = 0; j < DeepNum; j++) {
                if (i == (DeepNum - 1))
                    DeepValue += "┝";
                else
                    DeepValue += "--";
            }
            sysmoduleDto module = new sysmoduleDto();
            module.setId(data.getChildren().get(i).getId());
            module.setName(DeepValue + data.getChildren().get(i).getName());
            lst.add(module);
            lst.addAll(getListChildren(data.getChildren().get(i), DeepNum + 1));
        }
        return lst;
    }

    public static int parseInt(String s, int defaultValue) {
        if (s == null) return defaultValue;
        try {
            return Integer.parseInt(s);
        } catch (NumberFormatException x) {
            return defaultValue;
        }
    }

    public static long parseLong(String s, long defaultValue) {
        if (s == null) return defaultValue;
        try {
            return Long.parseLong(s);
        } catch (NumberFormatException x) {
            return defaultValue;
        }
    }

    public static List<sysmoduleDto> GetModule(List<sysmoduleDto> list, String id, int isid) {
        List<sysmoduleDto> module = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (isid == 1) {
                if (list.get(i).getId().toString().equals(id)) {
                    module.add(list.get(i));
                }
            } else {
                if (list.get(i).getParentid().toString().equals(id)) {
                    module.add(list.get(i));
                }
            }
        }
        return module;
    }


    //输出json返回信息
    public static String ReturnJsoninfo(String errstr) {
        String ret = "";
        String message = "";
        String msgcontents = "";
        if (errstr == null || errstr.length() > 0) {
            message = "nook";
            msgcontents = errstr;
        } else {
            message = "ok";
            msgcontents = "操作成功";
        }
        Map<String, Object> info = new HashMap<>();
        info.put("Message", message);
        info.put("Content", msgcontents);
        ret = JSON.toJSONString(info);
        return ret;
    }
}
